package com.tbm.console.proj.service.impl;

import com.tbm.console.proj.entity.TestEntity;
import com.tbm.console.proj.repository.TestRepository;
import com.tbm.console.proj.service.TestService;
import com.tbm.console.util.PageData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


//说明 字符串：，作为替换字符串 LOWERTest                Test

@Service
public class TestServiceImpl implements TestService {

    @Autowired
    private TestRepository testRepository;

    @Override
    public List<TestEntity> findAll() {
        return findAll(new PageData());
    }

    @Override
    public List<TestEntity> findAll(Integer start, Integer length) {
        return findAll(new PageData(),start,length);
    }

    @Override
    public List<TestEntity> findAll(PageData pageData) {
        Specification<TestEntity> sp = this.getTestSpecification(pageData);
        return testRepository.findAll(sp);
    }

    @Override
    public List<TestEntity> findAll(PageData pageData, Integer start, Integer length) {
        Specification<TestEntity> sp = this.getTestSpecification(pageData);
        Sort sort = new Sort(Sort.Direction.ASC,"id");
        Pageable pageable = new PageRequest(start, length, sort);
        return testRepository.findAll(sp,pageable).getContent();
    }

    @Override
    public long count() {
        return count(new PageData());
    }

    @Override
    public long count(TestEntity test) {
        return testRepository.count(Example.of(test));
    }

    @Override
    public long count(PageData pageData) {
        Specification<TestEntity> sp = this.getTestSpecification(pageData);
        return testRepository.count(sp);
    }

    @Override
    public boolean deleteById(Integer id) {
        testRepository.deleteById(id);
        return false;
    }

    @Override
    public boolean deleteByIds(Iterable<TestEntity> iterable) {
        return false;
    }

    @Override
    public TestEntity save(TestEntity test) {
		List<TestEntity> list = saveAll(Arrays.asList(test));
        return list.size() == 0 ? new TestEntity() : list.get(0);
    }

    @Override
    public List<TestEntity> saveAll(List<TestEntity> tests) {
        return testRepository.saveAll(tests);
         
    }

    @Override
    public TestEntity findById(Integer id) {
        TestEntity test = new TestEntity();
        try{
            test = testRepository.findById(id).get();
        }catch (Exception e){

        }
        return test;
    }

    @Override
    public List<TestEntity> findAllById(Integer[] ids) {
        return testRepository.findAllById(Arrays.asList(ids));
    }

    //通过Specification来获取自定义的查询条件
    private Specification<TestEntity> getTestSpecification(PageData pageData){
        return new Specification() {
            @Override
            public javax.persistence.criteria.Predicate toPredicate(Root root, CriteriaQuery criteriaQuery, CriteriaBuilder criteriaBuilder) {
                List<Predicate> preList = new ArrayList<Predicate>();
                if (null!=pageData.get("deleteFlag")) {
                    preList.add(criteriaBuilder.equal(root.get("deleteFlag"), pageData.get("deleteFlag").toString()));
                }
                
                Predicate[] p = new Predicate[preList.size()];
                return criteriaBuilder.and(preList.toArray(p));
            }
        };
    }
}